package com.lanou3g.code0426;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by dllo on 18/4/26.
 */
public class JDBCUtil {
    
    private static Connection conn;  //连接对象

    static {
         //静态代码块
         //当程序第一次加载这个类的时候,就会先执行这个类的 静态代码块
         // 并且整个项目只要不停,只会执行一次
         // 此块的作用: 加载mysql驱动,执行一次即可

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lan_ou?","root","123456");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    
    //获取statement
    public static Statement getStatement(){
        try {
            Statement statement = conn.createStatement();

            return statement;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    public static void closeConn(){
        if (conn != null){
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void closeResource(Statement statment){
        if (statment != null){
            try {
                statment.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }




    }



}
